package kr.pe.johnkim.waiting.mobile.controller;

import kr.pe.johnkim.waiting.mobile.domain.WaitingMember;
import kr.pe.johnkim.waiting.mobile.service.WaitingMemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Waiting 멤버 관련 controller 클래스 이다.
 *
 * @author : John_Kim <cale9797@gmail.com>
 * @date : 13. 4. 10.
 */
@Controller
@RequestMapping(value = "/waitingMemberController")
public class WaitingMemberController {

    // SLF4J Logging
    private Logger logger = LoggerFactory.getLogger(WaitingMemberController.class);

    @Autowired
    private WaitingMemberService waitingMemberService;

    /**
     * 기존에 등록된 정보가 있는지 확인 한다.
     *
     * @param waitingMember
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/selectDuplicate", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public WaitingMember selectDuplicate(@RequestBody WaitingMember waitingMember) throws Exception {
        logger.info("selectDuplicate");

        logger.info("qrCode : {}", waitingMember.getQrCode());

        return this.waitingMemberService.selectDuplicate(waitingMember);
    }

    /**
     * 멤버를 등록 한다.
     *
     * @param waitingMember
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/registerMember", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public WaitingMember registerMember(@RequestBody WaitingMember waitingMember) throws Exception {
        logger.info("registerMember");

        logger.info("qrCode                : {}", waitingMember.getQrCode());
        logger.info("phoneNumber           : {}", waitingMember.getPhoneNumber());
        logger.info("deviceType            : {}", waitingMember.getDeviceType());
        logger.info("registrationId        : {}", waitingMember.getRegistrationId());
        logger.info("receiveNotificationYn : {}", waitingMember.getReceiveNotificationYn());
        logger.info("activateYn            : {}", waitingMember.getActivateYn());
        logger.info("modifyDate            : {}", waitingMember.getModifyDate());
        logger.info("registerDate          : {}", waitingMember.getRegisterDate());

        WaitingMember result = new WaitingMember();

        try {
            this.waitingMemberService.registerMember(waitingMember);
            result.setResult("SUCCESS");
            logger.info("success to register member");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to register member.");
        }

        return result;
    }

    /**
     * 멤버 정보를 가져 온다.
     *
     * @param waitingMember
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/selectMemberInformation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public WaitingMember selectMemberInformation(@RequestBody WaitingMember waitingMember) throws Exception {
        logger.info("selectMemberInformation");

        logger.info("qrCode     : {}", waitingMember.getQrCode());
        logger.info("activateYn : {}", waitingMember.getActivateYn());

        return this.waitingMemberService.selectMemberInformation(waitingMember);
    }

    /**
     * 멤버 정보를 수정 한다. 현재는 이메일 주소뿐이다.
     *
     * @param waitingMember
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateMemberInformation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public WaitingMember updateMemberInformation(@RequestBody WaitingMember waitingMember) throws Exception {
        logger.info("updateMemberInformation");

        WaitingMember result = new WaitingMember();

        try {
            this.waitingMemberService.updateMemberInformation(waitingMember);
            result.setResult("SUCCESS");
            logger.info("success to update member information.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to update member information.");
        }

        return result;
    }

    /**
     * 멤버 목록을 가져 온다.
     *
     * @param waitingMember
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/selectMemberList", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<WaitingMember> selectMemberList(@RequestBody WaitingMember waitingMember) throws Exception {
        logger.info("selectMemberList");
        return this.waitingMemberService.selectMemberList(waitingMember);
    }

    /**
     * 멤버 총 갯수를 가져 온다.
     * @param waitingMember
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/selectMemberTotalCount", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public WaitingMember selectMemberTotalCount(@RequestBody WaitingMember waitingMember) throws Exception {
        logger.info("selectMemberTotalCount");
        return this.waitingMemberService.selectMemberTotalCount(waitingMember);
    }

    /**
     * 멤버의 GCM Registration Id / APNS Device Token 을 업데이트 한다.
     *
     * @param waitingMember
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateRegistrationId", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public WaitingMember updateRegistrationId(@RequestBody WaitingMember waitingMember) throws Exception {
        logger.info("updateRegistrationId");

        WaitingMember result = new WaitingMember();

        try {
            this.waitingMemberService.updateRegistrationId(waitingMember);
            result.setResult("SUCCESS");
            logger.info("success to update registration id.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to update registration id.");
        }

        return result;
    }

    /**
     * 멤버의 Push Notification 수신 여부를 업데이트 한다.
     *
     * @param waitingMember
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateReceiveNotificationYn", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public WaitingMember updateReceiveNotificationYn(@RequestBody WaitingMember waitingMember) throws Exception {
        logger.info("updateReceiveNotificationYn");

        WaitingMember result = new WaitingMember();

        try {
            this.waitingMemberService.updateReceiveNotificationYn(waitingMember);
            result.setResult("SUCCESS");
            logger.info("success to update receive notification yn.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to update receive notification yn.");
        }

        return result;
    }

    /**
     * 멤버의 활성화 여부를 업데이트 한다.
     *
     * @param waitingMember
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateMemberActivateYn", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public WaitingMember updateMemberActivateYn(@RequestBody WaitingMember waitingMember) throws Exception {
        logger.error("updateMemberActivateYn");

        WaitingMember result = new WaitingMember();

        try {
            this.waitingMemberService.updateMemberActivateYn(waitingMember);
            result.setResult("SUCCESS");
            logger.info("success to update member activate yn.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to update member activate yn.");
        }

        return result;
    }
}
